/*
 * TeacherPhoneDbSaver.cpp
 *
 *  Created on: 04 ���� 2010
 *      Author: Sartios
 */

#include "TeacherPhoneDbSaver.h"

#include "../../../OdbcConnectionSingleton/OdbcConnectionSingleton.h"
#include "../../../DbElementProvider/DbElementProvider.h"

#include "../../../../Validators/PhoneValidators/PhoneNumberValidators/HomeTelephoneNumberValidator/HomeTelephoneNumberValidator.h"
#include "../../../../Validators/PhoneValidators/PhoneCategoryValidators/TelephoneCategoryValidator/TelephoneCategoryValidator.h"
#include "../../../../Validators/TeacherValidators/TeacherInsuranceNumberValidator/AfmPolicy/AfmPolicy.h"

TeacherPhoneDbSaver::TeacherPhoneDbSaver() {
	connect();

}

TeacherPhoneDbSaver::~TeacherPhoneDbSaver() {
	//empty
}

bool	TeacherPhoneDbSaver::saveTeacherPhone(const string *insuranceNumber,const string *number,const string *category){
	if((NULL!=insuranceNumber)&&(NULL!=number)&&(NULL!=category)){

		ITeacherInsuranceNumberValidator *insuranceNumberValidator = new AfmPolicy;
		if(insuranceNumberValidator->insuranceNumberAgreeWithBusinessPolicy(insuranceNumber)){
			if(true==phoneContentAgreeWithBusinessPolicy(insuranceNumber,number,category)){

				SQLHANDLE stmtHandle = OdbcConnectionSingleton::getInstance()->getStmtHandle();
				if(NULL!=stmtHandle){
					SQLRETURN rc;
					bool insertionSucceed=false;
					string sqlString = "INSERT INTO teacherphones (phone,category,TeacheD_afm) VALUES ('"
							+ (*number) +					"','"
							+ (*category) +					"','"
							+ (*insuranceNumber) +			"');";

					rc = SQLExecDirect(stmtHandle,(SQLCHAR *)sqlString.c_str(),SQL_NTS);

					if(rc == SQL_SUCCESS){
						insertionSucceed = true;
						return insertionSucceed;
					}
					return insertionSucceed;
				}
			}
		}
		delete insuranceNumberValidator;
	}
	return false;
}

bool TeacherPhoneDbSaver::connect(){
	const string DB_NAME = *(DbElementProvider::getInstance()->getDbName());
	const string USERNAME = *(DbElementProvider::getInstance()->getUsername());
	const string PASSWORD = *(DbElementProvider::getInstance()->getPassword());
	bool connectionEstablished = OdbcConnectionSingleton::getInstance()->connect(&DB_NAME,&USERNAME,&PASSWORD);
	if(false==connectionEstablished){
		throw;
	}
	return true;
}

bool	TeacherPhoneDbSaver::phoneContentAgreeWithBusinessPolicy(const string *insuranceNumber,const string *number,const string *category){
	bool contentIsCorrect = false;
	if((NULL!=insuranceNumber)&&(NULL!=number)&&(NULL!=category)){

		IPhoneNumberValidator *numberValidator=new HomeTelephoneNumberValidator;
		if((numberValidator->phoneNumberHasAppropriateContent(number))&&(numberValidator->phoneNumberHasAppropriateLength(number))){

			ITelephoneCategoryValidator *categoryValidator = new TelephoneCategoryValidator;
			if(categoryValidator->phoneCategoryIsCorrect(category)){
				contentIsCorrect = true;
			}
			delete categoryValidator;
		}
		delete numberValidator;
	}
	return contentIsCorrect;
}
